<?php
/**
 * Created by PhpStorm.
 * User: z
 * Date: 14-8-30
 * Time: 下午6:53
 */
class supplierskus
{

    private static function getClient($action) {
        require_once(Kohana::find_file('vendor','phprpc/phprpc_client'));
        $server_url = Kohana::$config->load('server_url');
        $client     = new PHPRPC_Client($server_url['server'].'/newerpserverapi/'.$action);
        return $client;
    }
    
    // 统计
    public static function count($condtion) {
        $sql = "SELECT COUNT(1) AS num FROM vmi_supplier_skus WHERE isavailable=1 ";
        if(isset($condtion['supplier_name']) && !empty($condtion['supplier_name'])) {
            $supplier_id = (int)self::getSupplierIdByName($condtion['supplier_name']);
            $sql .= " AND supplier_id={$supplier_id}";
        }
        if(isset($condtion['sku']) && !empty($condtion['sku'])) {
            $sql .= " AND sku='" . addslashes($condtion['sku']) . "'";
        }
        if(isset($condtion['id']) && !empty($condtion['id'])) {
            $sql .= " AND id IN (" . implode(',', array_map('intval', (array)$condtion['id'])) . ")";
        }
        return DB::query(Database::SELECT, $sql)->execute('newerp')->get('num');
    }
    
    public static function query($condtion) {
        $sql = "SELECT * FROM vmi_supplier_skus WHERE isavailable=1 ";
        if(isset($condtion['supplier_name']) && !empty($condtion['supplier_name'])) {
            $supplier_id = (int)self::getSupplierIdByName($condtion['supplier_name']);
            $sql .= " AND supplier_id={$supplier_id}";
        }
        if(isset($condtion['sku']) && !empty($condtion['sku'])) {
            $sql .= " AND sku='" . addslashes($condtion['sku']) . "'";
        }
        if(isset($condtion['id']) && !empty($condtion['id'])) {
            $sql .= " AND id IN (" . implode(',', array_map('intval', (array)$condtion['id'])) . ")";
        }
        $sql .= " ORDER BY id DESC";
        if(isset($condtion['limit']) && !empty($condtion['limit'])) {
            $offset = intval($condtion['offset']);
            $limit  = intval($condtion['limit']);
            $sql .= " LIMIT {$offset},{$limit}";
        }
        return DB::query(Database::SELECT, $sql)->execute('newerp')->as_array();
    }
    
    public static function do_select($condtion){
        $reslut = DB::select('*')->from('vmi_supplier_skus')->where('isavailable','=','1');
        if(isset($condtion['sku'])) {
            $reslut = $reslut->where('sku','=',$condtion['sku']);
        }
        if(isset($condtion['id']) && !empty($condtion['id'])){
            $reslut = $reslut->where('id','in',$condtion['id']);
        }

        if(isset($condtion['supplier_id'])) {
            $reslut = $reslut->where('supplier_id','=',$condtion['supplier_id']);
        }
        if(isset($condtion['supplier_name'])) {
            $reslut = $reslut->where('supplier_name','like','%'.$condtion['supplier_name'].'%');
        }
        return $reslut = $reslut->order_by('id','desc')->limit($condtion['limit'])->offset($condtion['offset'])->execute('newerp')->as_array();
    }
    /**
     * 描述:获取根据id供应商名称
     * 作者:kelvin
     */
    public static function getSupplierNameById($ids)
    {
        $client    = self::getClient('putSuppliers');
        $suppliers = $client->getinfo(array('id','name'),array('id'=>'in ('.implode(",",$ids).')'));
        $sus = array();
        foreach ($suppliers as &$v){
            $sus[$v['id']] = $v['name'];
        }
        return $sus;
    }
    /**
     * 描述:获取供应商
     * 作者:kelvin
     */
    public static function getSuppliers($fields,$condtions,$limit = NULL)
    {
        $client    = self::getClient('putSuppliers');
        $suppliers = $client->getinfo($fields,$condtions,$limit);
        return $suppliers;
    }
    /**
     * 描述:根据id获取供应商名称
     * 作者:chamlinsun
     */
    public static function getNameById($id){
        $client    = self::getClient('putSuppliers');
        $suppliers = $client->getinfo(array('name'),array('id'=>'=' .$id));
        $name = count($suppliers)?$suppliers[0]['name']:'';
        
        return $name;
    }
    
    /**
     * 描述:根据供应商名称获取供应商Id
     * 作者:chamlinsun
     */
    public static function getSupplierIdByName($name){   
        $supplier_id = DB::select('supplier_id')->from('vmi_supplier_skus')->where('supplier_name','=',$name)->execute('newerp')->get('supplier_id');
        return intval($supplier_id);
    }
    /**
     * 描述:根据id获取vmi供应商名称
     * 作者:kelcin
     */
    public static function getNameByIdForVmi($id){
        $suppliers    = DB::select('supplier_name')->from('vmi_supplier_skus')->where('supplier_id','=',$id)->execute('newerp')->current();
        $name = $suppliers?$suppliers['supplier_name']:'';
        return $name;
    }
    /**
     *  根据供应商sku获取加点税率
     *  作者:kelvin
     * */
    public static function getRealTaxRate($supplierId,$sku)
    {
        $name = 0;
        if($supplierId && $sku){
            $suppliers    = DB::select('real_tax_rate')->from('vmi_supplier_skus')->where('supplier_id','=',$supplierId)->where('sku','=',trim($sku))->execute('newerp')->current();
            $name = $suppliers?$suppliers['real_tax_rate']:'';
        }
        return $name;
    }
    /**
     *  获取供应商联系人与地址
     *  作者:kelvin
     * */
    public static function getSupplierAddress($id = null)
    {
        require_once(Kohana::find_file('vendor','phprpc/phprpc_client'));
        $server_url = Kohana::$config->load('server_url');
        $client     = new PHPRPC_Client($server_url['server'].'/accountsystemapi/query');
        $sql      = "SELECT c.id,phone,fax,contact,mobile,qq,wangwang,address
                        FROM supplier_contacts sc,contacts c
                        WHERE sc.contact_id = c.id AND sc.isavailable=1 AND sc.supplier_id = ".$id;
        $suppliers = $client->getinfo($sql);
        return $suppliers?$suppliers[0]:array();
    }
}